package com.putong35.common.app.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.putong35.common.app.data.UrlConfig;
import com.putong35.common.baseactfrag.fragment.SwipeListFragment;
import com.putong35.common.httprequest.IHttpRequest;
import com.putong35.common.httprequest.UrlParams;
import com.putong35.common.httprequest.app.MyHttpRequest;
import com.putong35.common.httprequest.responsecallbacks.ListBeanResponseListener;

import java.util.List;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public abstract class AppListFragment extends SwipeListFragment {
    public static final String TAG = AppListFragment.class.getSimpleName();

    private static final int START_PAGE = 1;
    private static final int PAGE_SIZE = 10;
    private boolean loadWhenCreate = true;

    protected int mPage;
    private ListBeanResponseListener mResponseListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
//        clearDecorations();
        setPullToRefreshEnable(true);
        init();
        if (loadWhenCreate) {
            startLoading();
        }
        return view;
    }

    private void init() {
        mPage = START_PAGE;
        mResponseListener = makeResponseListener();

    }


    @Override
    public void onStartLoading() {
        if (TextUtils.isEmpty(mUrl)) {
            throw new RuntimeException("请先配置请求的URL地址");
        }
        if (mResponseListener.hasNextPage()) {
            UrlParams urlParams = makeParams();
            if (urlParams == null) {
                urlParams = new UrlParams();
            }
            if (!urlParams.getUrlParamMap().containsKey(UrlConfig.Param.PAGE_SIZE)) {
                urlParams.put(UrlConfig.Param.PAGE_SIZE, PAGE_SIZE);
            }
            urlParams.put(UrlConfig.Param.PAGE, mPage);


            IHttpRequest request = new MyHttpRequest(getActivity());
            if (mMethod == Request.Method.GET) {
                request.get(mUrl, urlParams, mResponseListener);
            } else if (mMethod == Request.Method.POST) {
                request.post(mUrl, urlParams, mResponseListener);
            } else {
                throw new RuntimeException("不支持请求方法：" + mMethod);
            }
        /*    HttpCommon.httpGetList(mUrl, null, CollectionBean.class, new OnSuccessListener<List<CollectionBean>>() {
                @Override
                public void onSuccess(int code, String msg, List<CollectionBean> data, Object extObj) {
                    Log.d(TAG, "onSuccess: ");
                    onListDataUpdateSuccess(data);
                }
            }, new OnFailureListener() {
                @Override
                public void onFailure(int errCode, String msg) {
                    Log.d(TAG, "onFailure: ");
                    EasyToast.showToast(getActivity(), msg);
                    onListDataUpdateFailed(errCode, msg);
                }
            });*/
        }
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        reload();
    }

    public void reload() {
        getData().clear();
        getAdapter().notifyDataSetChanged();
        init();
        startLoading();
    }

    public UrlParams makeParams() { return new UrlParams();}

    public abstract ListBeanResponseListener makeResponseListener();

    private int mMethod = Request.Method.GET;
    private String mUrl = "";
    public void setRequestInfo(int method, String url) {
        this.mMethod = method;
        this.mUrl = url;
    }

    @Override
    public void onListDataUpdateSuccess(List list) {
        super.onListDataUpdateSuccess(list);
        if (list != null && list.size() != 0) {
            mPage++;
        }
    }

    public void setLoadWhenCreate(boolean loadWhenCreate) {
        this.loadWhenCreate = loadWhenCreate;
    }
}
